<template>
  <view>
    <nav-bar title="初期评定结果"></nav-bar>
    <view class="plan-form">
      <scroll-view scroll-y :style="{ height: formHeight }">
        <view class="form-box" v-if="showForm">
          <you-form
            ref="form"
            :formTitle="formTitle"
            :formConfig="assessConfig"
            v-model="assessData"
          ></you-form>
        </view>
      </scroll-view>
    </view>
    <you-bottom-btn
      v-if="showBottomBtn"
      :btnConfig="bottomBtnConfig"
      @clickSubmit="clickSubmit"
      @clickCancel="clickCancel"
    ></you-bottom-btn>
    <you-popup
      :popupVisible="popupVisible"
      popupTitle="提示"
      :showBtn="false"
      @cancel="popupVisible = false"
      @popupClose="popupVisible = false"
      @confirm="confirmDelete"
    >
      <view slot="popupContent">
        <view class="content">需要继续编辑训练计划吗</view>
        <view class="popup-btn">
          <view class="cancel" @click="clickNoEdit">不用了</view>
          <view class="edit" @click="clickEdit">去编辑</view>
        </view>
      </view>
    </you-popup>
  </view>
</template>

<script>
  import { ASSESS_CONFIG } from '../formConfig/planConfig'
  import api_physician from '@/api/physician'
  import api_user from '@/api/user'
  export default {
    name: 'AssessReport',
    data() {
      return {
        formHeight: '',
        formTitle: '',
        medicineId: null,
        planId: null,
        assessConfig: [],
        assessData: {},
        bottomBtnConfig: [
          { text: '取消', class: 'cancel', funcName: 'clickCancel' },
          { text: '确认提交', class: 'confirm', funcName: 'clickSubmit' }
        ],
        popupVisible: false,
        showBottomBtn: true,
        showForm: false
      }
    },
    onLoad(options) {
      this.formHeight = this.$store.getters.heightData.noTabMainHeight - 85 + 'px'
      this.assessConfig = uni.$u.deepClone(ASSESS_CONFIG)
      this.formTitle = '患者：' + options.patientName
      if (options.planId) {
        this.planId = options.planId
      }
      if (options.medicineId) {
        this.medicineId = options.medicineId
      }
      if (options.justView) {
        this.showBottomBtn = false
        this.assessConfig.forEach(item => {
          item.isDisabled = true
        })
        this.scrollHeight = this.$store.getters.heightData.noTabMainHeight - 10 + 'px'
        this.getAssessData()
      } else {
        this.showForm = true
      }
    },
    methods: {
      getAssessData() {
        const params = {
          planId: this.planId,medicineId: this.medicineId || null
        }
        api_user.reportDetail(params).then(res => {
          if (res.code !== 200) return
          this.assessData = res.data
          this.showForm = true
        })
      },
      clickCancel() {
        uni.redirectTo({
          url: `/subPages/physicianTrainPlan/patient/patientDetail?medicineId=${this.medicineId}`
        })
      },
      async clickSubmit() {
        const formCheck = await this.$refs.form.checkFormData()
        if (!formCheck) return uni.$u.toast('请将表单数据填写完整')
        if (!this.assessData.faceImg) return uni.$u.toast('请上传正面照')
        if (!this.assessData.backImg) return uni.$u.toast('请上传背面照')
        if (!this.assessData.sideImg) return uni.$u.toast('请上传侧面照')
        const params = {
          ...this.assessData,
          medicineId: this.medicineId,
          planId: this.planId
        }
        api_physician.addReport(params).then(res => {
          if (res.code !== 200) return
          uni.showToast({
            title: '评估报告新增成功！',
            icon: 'success',
            mask: true
          })
          this.popupVisible = true
        })
      },
      clickNoEdit() {
        this.popupVisible = false
        uni.redirectTo({
          url: `/subPages/physicianTrainPlan/patient/patientDetail?medicineId=${this.medicineId}`
        })
      },
      clickEdit() {
        this.popupVisible = false
        uni.navigateTo({
          url: `/subPages/physicianTrainPlan/plan/addPlan?medicineId=${this.medicineId}`
        })
      }
    }
  }
</script>
<style lang="scss">
  .plan-form {
    padding-top: 15px;
  }
  .form-box {
    margin: 0 30rpx;
    border-radius: 16rpx;
    background-color: #FFF;
    overflow: hidden;
  }
  .content {
    margin-bottom: 50rpx;
    text-align: center;
  }
  .popup-btn {
    width: 100%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 88rpx;
    border-top: 2rpx solid #e5e5e5;
    .cancel,
    .edit {
      width: 100%;
      height: 100%;
      line-height: 88rpx;
    }
    .edit {
      border-left: 2rpx solid #e5e5e5;
      color: #115BAA;
    }
  }
</style>
